//
//  MoreViewController.swift
//  yaokongqi
//
//  Created by HappyMilk 02 on 2017/9/26.
//  Copyright © 2017年 nike. All rights reserved.
//

import UIKit

class MoreViewController: UIViewController {

    
    @IBOutlet weak var lab_username: UILabel!
    @IBOutlet weak var imgv_userhead: UIImageView!
    
    var userModel: MJUserModel!
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        MJUserModel.registerSubclass()
        
        let open = UserDefaults.standard.object(forKey: "user") as? String
        if open == "1" {
            if (UserDefaults.standard.object(forKey: "first") != nil) {
                findUserModel()
            }else{
                self.createUserModel()
            }
        }
    }
    
    func findUserModel() {
        let query = AVQuery.init(className: "User_list")
        let objectid = UserDefaults.standard.object(forKey: "first") as! String
        query.getObjectInBackground(withId: objectid) { ( object, error) in
            self.userModel = object as! MJUserModel
            self.lab_username.text = self.userModel.name
            self.imgv_userhead.sd_setImage(with: URL.init(string: self.userModel.head_img.url!))
            print("----- 查找用户信息成功")
        }
    }
    func createUserModel() {
        
        let model = MJUserModel.init()
        model.user_id = is_chinese() ? "" : ""
        model.name = is_chinese() ? "汤姆" : "Tomi"
        model.phoneNum = is_chinese() ? "暂无" : "noting"
        model.sex = is_chinese() ? "暂无" : "noting"
        model.adrs = is_chinese() ? "暂无" : "noting"
        model.adrs_now = is_chinese() ? "暂无" : "noting"
        model.birthday = is_chinese() ? "暂无" : "noting"
        model.height = is_chinese() ? "暂无" : "noting"
        model.school_record = is_chinese() ? "暂无" : "noting"
        model.enmotion = is_chinese() ? "暂无" : "noting"
        model.signature = is_chinese() ? "暂无" : "noting"
        let img = UIImage.init(named: "user-head")
        let data = UIImagePNGRepresentation(img!)
        let file = AVFile.init(data: data!)
        model.head_img = file
        model.saveInBackground { (succeeded, error) in
            if succeeded {
                UserDefaults.standard.set(model.objectId, forKey: "first")
                self.lab_username.text = model.name
                self.imgv_userhead.image = UIImage.init(named: "user-head")
                print("----- 成功创建用户信息")
            }
            print("---- \(String(describing: error))")
        }
        userModel = model
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    
    @IBAction func loginAction(_ sender: Any) {
        
        AdLibrary.stander().showAd()
        
        let open = UserDefaults.standard.object(forKey: "user") as? String
        if open == "1" {
            let sb = UIStoryboard.init(name: "mine", bundle: nil)
            let vc: MJMyMsgController = sb.instantiateViewController(withIdentifier: "MJMyMsgController") as! MJMyMsgController
            vc.userModel = userModel
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        let sb = UIStoryboard.init(name: "login", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "LoginViewController")
        present(vc, animated: true, completion: nil)
        
    }
    
    
    @IBAction func pushMessageController(_ sender: UIButton) {
        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MessageViewController")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func pushAboutController(_ sender: Any) {
        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewController")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func pushSettingController(_ sender: Any) {
        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingViewController")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
